package com.jn.exam.question.service.impl;

import com.jn.exam.HTMLUtil;
import com.jn.exam.question.entity.Question;
import com.jn.exam.question.entity.vo.QuestionWrapper;
import com.jn.exam.question.mapper.QuestionMapper;
import com.jn.exam.question.service.OssFeignService;
import com.jn.exam.question.service.QuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 总题库 服务实现类
 * </p>
 *
 * @author 江南大学1033190417
 * @since 2022-01-17
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Autowired
    private OssFeignService ossFeignService;

    //通过id彻底删除
    @Async
    @Override
    public Future<Boolean> completeDeleteById(String id) {
        Question question = baseMapper.getDeletedById(id);
        String name = question.getName();
        String answer = question.getAnswer();
        List<String> nameUrl = HTMLUtil.getUrl(name);
        List<String> answerUrl = HTMLUtil.getUrl(answer);
        for (String url : nameUrl) {
            boolean b = ossFeignService.deleteFileByUrl(url);
            if (!b) {
                return new AsyncResult<>(false);
            }
        }
        for (String url : answerUrl) {
            boolean b = ossFeignService.deleteFileByUrl(url);
            if (!b) {
                return new AsyncResult<>(false);
            }
        }
        boolean b = baseMapper.completeDeleteById(id);
        if (b) {
            return new AsyncResult<>(true);
        }
        return new AsyncResult<>(false);
    }

    @Override
    public boolean completeDeleteByIds(String[] ids) {
        return baseMapper.completeDeleteByIds(ids);
    }

    //分页查询
    @Override
    public List<Question> pageSelect(QuestionWrapper questionWrapper) {
        return baseMapper.pageSelect(questionWrapper);
    }

    @Override
    public Integer getPageTotal(QuestionWrapper questionWrapper) {
        return baseMapper.getPageTotal(questionWrapper);
    }

    //查询已删除

    @Override
    public List<Question> getDeletedQuestions() {
        return baseMapper.getDeletedQuestions();
    }

    @Override
    public boolean recoveryDeleted(String[] ids) {
        return baseMapper.recoveryDeleted(ids);
    }

    @Override
    public boolean deleteQuestionExpire(Date date) {
        return baseMapper.deleteQuestionExpire(date);
    }


    @Override
    public boolean removeByIds(String[] ids) {
        Map<String, Object> params = new HashMap<>();
        params.put("ids", ids);
        params.put("updateTime", new Date());
        return baseMapper.removeByIds(params);
    }

    @Async
    @Override
    public Future<Boolean> deleteQuestionFile(String name, String answer) {
        List<String> nameUrl = HTMLUtil.getUrl(name);
        List<String> answerUrl = HTMLUtil.getUrl(answer);
        if (!nameUrl.isEmpty()) {
            for (String url : nameUrl) {
                boolean deleteFileByUrl = ossFeignService.deleteFileByUrl(url);
                if (!deleteFileByUrl) {
                    return new AsyncResult<>(false);
                }
            }
        }
        if (!answerUrl.isEmpty()) {
            for (String url : answerUrl) {
                boolean deleteFileByUrl = ossFeignService.deleteFileByUrl(url);
                if (!deleteFileByUrl) {
                    return new AsyncResult<>(false);
                }
            }
        }
        return new AsyncResult<>(true);
    }


    public boolean deleteNoExistUrl(String old, String now) {
        List<String> l1 = HTMLUtil.getUrl(old);
        List<String> l2 = HTMLUtil.getUrl(now);
        for (String url : l1) {
            if (!l2.contains(url)) {
                boolean b = ossFeignService.deleteFileByUrl(url);
                if (!b) {
                    return false;
                }
            }
        }
        return true;
    }
}
